package com.lfy.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lfy.conf.MqConf;
import com.lfy.domian.ErrorMqEntity;
import com.lfy.domian.User;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class rabbitmqController {
    @Resource
    RabbitTemplate rabbitTemplate;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @GetMapping("/send")
    public String sendMsg(){
        rabbitTemplate.convertAndSend(MqConf.Mq_B,"tt","halo",new CorrelationData("1"));

        return "ok";
    }
    @GetMapping("/senduser")
    public String sendUser(){
        rabbitTemplate.convertAndSend(MqConf.Mq_B,"tt",new User("1","2","3","3","5","3","2"),new CorrelationData("1"));

        return "ok";
    }
    @GetMapping("/sendusers")
    public String sendUsers(){
        rabbitTemplate.convertAndSend(MqConf.Mq_B,"ttt",new User("1","2","3","3","5","3","2"),new CorrelationData("1"));

        return "ok";
    }
    @GetMapping("/senduserss")
    public String sendUserss(){
        rabbitTemplate.convertAndSend(MqConf.Mq_Q,"t",new User("1","2","3","3","5","3","2"),new CorrelationData("1"));

        return "ok";
    }
    @SneakyThrows
    @GetMapping("/testmq")
    public String mq(){
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        Object error1 = valueOperations.get("ErrorNew");
//
        ObjectMapper objectMapper = new ObjectMapper();
        ErrorMqEntity errorMqEntity = objectMapper.readValue(error1.toString(), new TypeReference<ErrorMqEntity>() {
        });

        rabbitTemplate.convertAndSend(errorMqEntity.getB(),errorMqEntity.getRouteKey(),errorMqEntity.getObject());


        System.out.println("路由："+errorMqEntity.getRouteKey());
        System.out.println("交换机："+errorMqEntity.getB());
        System.out.println();
        return "ok";
    }
}
